#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
#include<string.h>
#include<sys/wait.h>
#include<stdio.h>
int p1,p2;
main( )
    { 
int fd[2];
char buf_out [100], buf_in [100];
pipe(fd);  /*创建一个管道*/
while ((p1=fork( ))==-1);
if(p1==0)
{
lockf(fd[1],1,0);
strcpy(buf_out,"child P1 process is sending message!");  /*把串放入数组buf_out中*/
write(fd[1],buf_out,50);     /*向管道写长为50字节的串*/
sleep(5);        /*自我阻塞5秒*/
lockf(fd[1],0,0);
exit(0);
        }
else
        {
while((p2=fork( ))==-1);
if(p2==0)
{    lockf(fd[1],1,0);           /*互斥*/
   sprintf(buf_out,"child %d process is sending message!",getpid());
                 write(fd[1], buf_out,50);
                 sleep(5);
           lockf(fd[1],0,0);
                 exit(0);
            }
            else
            {    
       waitpid(p1,NULL,0);       /*同步*/
       read(fd[0], buf_in,50);   
                 printf("%s\n", buf_in);
       waitpid(p2,NULL,0);
       read(fd[0], buf_in,50);
       printf("%s\n", buf_in);
                 exit(0);
             }
        }
}
